package com.yuuzy.service.impl;

import com.yuuzy.entity.PatientImageInfo;
import com.yuuzy.mapper.PatientImageInfoMapper;
import com.yuuzy.service.ImageService;
import com.yuuzy.utils.AliOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Service
@Slf4j
public class ImageServiceImpl implements ImageService {
	@Autowired
	private PatientImageInfoMapper patientImageInfoMapper;
	@Autowired
	private AliOssUtil aliOssUtil;

	/**
	 * 文件上传
	 *
	 * @param id
	 * @param file
	 * @return
	 */
	@Override
	public String upload(Integer id, MultipartFile file) {
		//原始文件名，由前端负责文件命名
		String originalFilename = file.getOriginalFilename();
//		//使用uuid构造文件名
//		String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
//		String objectName = UUID.randomUUID().toString() + extension;

		//文件的请求路径
		String filePath = null;
		try {
			filePath = aliOssUtil.upload(file.getBytes(), originalFilename);
			//病历命名规则：序号-姓名-扫描类别-时间
			PatientImageInfo patientImageInfo = new PatientImageInfo();
			patientImageInfo.setId(id);
			if (filePath.contains("门诊病历")) {
				patientImageInfo.setThisTimeScanpicture(filePath);
			} else if (filePath.contains("既往病历")) {
				patientImageInfo.setPreviousScanpicture(filePath);
			} else if (filePath.contains("面部图像")) {
				patientImageInfo.setFaceImage(filePath);
			} else if (filePath.contains("舌部图像")) {
				patientImageInfo.setTongueImage(filePath);
			}
			patientImageInfoMapper.updatePatientImage(patientImageInfo);
			return filePath;
		} catch (IOException e) {
			log.error("文件上传失败：{}", e);
		}
		return null;
	}

	/**
	 * 文件删除
	 *
	 * @param id
	 */
	@Override
	public void delete(Integer id) {
		log.info("删除患者云端图片,患者id：{}",id);
		//1.获取待删除的文件路径
		PatientImageInfo patientImageInfo = patientImageInfoMapper.selectById(id);

		//2.依次处理待删除的文件
		//处理门诊病历
		try {
			if (patientImageInfo.getThisTimeScanpicture() != null) {
				String imageUrl = patientImageInfo.getThisTimeScanpicture();
				String fileName = imageUrl.substring(imageUrl.lastIndexOf('/') + 1);
				aliOssUtil.delete(fileName);
			}
			//处理舌面图像
			if (patientImageInfo.getTongueImage() != null) {
				String imageUrl = patientImageInfo.getTongueImage();
				String fileName = imageUrl.substring(imageUrl.lastIndexOf('/') + 1);
				aliOssUtil.delete(fileName);
			}
			//处理既往病历
			if (patientImageInfo.getPreviousScanpicture() != null) {
				String imageUrl = patientImageInfo.getPreviousScanpicture();
				String fileName = imageUrl.substring(imageUrl.lastIndexOf('/') + 1);
				aliOssUtil.delete(fileName);
			}
			//处理面部图像
			if (patientImageInfo.getFaceImage() != null) {
				String imageUrl = patientImageInfo.getFaceImage();
				String fileName = imageUrl.substring(imageUrl.lastIndexOf('/') + 1);
				aliOssUtil.delete(fileName);
			}

		} catch (Exception e) {
			log.error("文件删除失败：{}", e);
		}
	}

}
